/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quan;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import javax.swing.plaf.metal.MetalInternalFrameTitlePane;

/**
 *
 * @author Quannk
 */
public class QInternalFrame extends JInternalFrame {

    int level;
    private JPopupMenu popupMenu;
    Point selectedPoint;
    int index;
    QInternalFrame parentFrame;

    public Point getSelectedPoint() {
        return selectedPoint;
    }

    public void setSelectedPoint(Point selectedPoint) {
        this.selectedPoint = selectedPoint;
    }

    public QInternalFrame(String title) {
        super(title, false, false, false, false);
        QInternalFrameUI qUI = new QInternalFrameUI(this);
        setUI(qUI);
        JLabel lb = new JLabel("Quannk");
        lb.setSize(getWidth(), 20);
        lb.setPreferredSize(new Dimension(getWidth(), 20));
        qUI.setSouthPane(lb);
//        setLayout(new BorderLayout());
        setBorder(null);
        initListener();
        initPopupMenu();
        setBackground(new Color(0, 0, 0, 0));

//        final QTitlePane qTitlePane = new QTitlePane(this);
//        qUI.setNorthPane(qTitlePane);
//        BasicInternalFrameUI frameUI = (BasicInternalFrameUI) getUI();        
//        final QTitlePane qTitlePane = new QTitlePane(this);
//        MetalInternalFrameTitlePane titlePa = (MetalInternalFrameTitlePane)frameUI.getNorthPane();
//        frameUI.setNorthPane(titlePa);
//        setUI(frameUI);


//        System.out.println(UIManager.getLookAndFeel().getDefaults().get("InternalFrame.inactiveTitleBackground"));
//        System.out.println(UIManager.getLookAndFeel().getDefaults().get("InternalFrame.activeTitleBackground"));
//        UIManager.getLookAndFeel().getDefaults().put("InternalFrame.activeTitleBackground", "java.awt.Color[r=0,g=0,b=0,a=0]");
//        UIManager.getLookAndFeel().getDefaults().put("InternalFrame.inactiveTitleBackground", "java.awt.Color[r=0,g=0,b=0,a=0]");
//        System.out.println(UIManager.getLookAndFeel().getDefaults().get("InternalFrame.inactiveTitleBackground"));
//        System.out.println(UIManager.getLookAndFeel().getDefaults().get("InternalFrame.activeTitleBackground"));
    }

    private void initPopupMenu() {
        popupMenu = new JPopupMenu("Menu");
        JMenuItem mniNew = new JMenuItem("New");
        mniNew.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((QDesktopPane) getDesktopPane()).addQInternalFrame(true, null);
            }
        });
        JMenuItem mniDelete = new JMenuItem("Delete");
        mniDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((QDesktopPane) getDesktopPane()).removeQInternalFrame(QInternalFrame.this);
            }
        });
        JMenuItem mniNewLine = new JMenuItem("New Line");
        mniNewLine.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((QDesktopPane) getDesktopPane()).startNewLine(QInternalFrame.this);
            }
        });
        popupMenu.add(mniNew);
        popupMenu.add(mniDelete);
        popupMenu.add(mniNewLine);
    }

    private void initListener() {
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                selectedPoint.x = getLocation().x + getWidth() / 2;
                selectedPoint.y = getLocation().y + getHeight() / 2;
                ((QDesktopPane) getDesktopPane()).updatePointWhenMove(QInternalFrame.this);
                getDesktopPane().repaint();

            }
        });
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                QDesktopPane desktopPane = (QDesktopPane) getDesktopPane();
                if (desktopPane.getIsStartNewLine()) {
                    desktopPane.createNewLine(desktopPane.getStartNewLineFrame(), QInternalFrame.this);
                }
                if (e.getButton() == MouseEvent.BUTTON3) {
                    popupMenu.show(QInternalFrame.this, e.getX(), e.getY());
                }

            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        try {
            BufferedImage bi1 = ImageIO.read(new File("Smile.png"));
            int titleHeight = ((QInternalFrameUI)getUI()).getNorthPane().getHeight();
            BufferedImage bi2 = getScaledImage(bi1, getWidth(), getHeight() - titleHeight);
            g.drawImage(bi2, 0, titleHeight, this);
        } catch (IOException ex) {
            Logger.getLogger(QInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        double scaleX = (double) width / imageWidth;
        double scaleY = (double) height / imageHeight;
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

        return bilinearScaleOp.filter(
                image,
                new BufferedImage(width, height, image.getType()));
    }
}
